<template>
  <div :class="{fullscreen:fullscreen}" class="tinymce-container" :style="{width:containerWidth}">
    <textarea :id="tinymceId" class="tinymce-textarea" />
    <div class="editor-custom-btn-container">
      <editor-image color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'Tinymce'
}
</script>

<script setup>
import {
  defineProps,
  ref,
  watch,
  nextTick,
  onMounted,
  defineEmits,
  onActivated,
  onDeactivated,
  onUnmounted,
  computed
} from 'vue'
import EditorImage from './components/EditorImage'
import plugins from './plugins'
import toolbar from './toolbar'
import load from './dynamicLoadScript'
import { ElMessage } from 'element-plus'

// why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
const tinymceCDN = 'https://cdn.jsdelivr.net/npm/tinymce-all-in-one@4.9.3/tinymce.min.js'

const props = defineProps({
  id: {
    type: String,
    default: function() {
      return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
    }
  },
  modelValue: {
    type: String,
    default: ''
  },
  toolbar: {
    type: Array,
    required: false,
    default() {
      return []
    }
  },
  menubar: {
    type: String,
    default: 'file edit insert view format table'
  },
  height: {
    type: [Number, String],
    required: false,
    default: 360
  },
  width: {
    type: [Number, String],
    required: false,
    default: 'auto'
  }
})

const hasChange = ref(false)
const hasInit = ref(false)
const tinymceId = ref(props.id)
const fullscreen = ref(false)
const languageTypeList = ref({
  'en': 'en',
  'zh': 'zh_CN',
  'es': 'ex_MX',
  'ja': 'ja'
})

const containerWidth = computed(() => {
  const width = props.width
  if (/^[\d]+(\.[\d]+)?$/.test(width)) { // matches '100', '100'
    return `${width}px`
  }
  return width
})

watch(() => props.value, val => {
  if (!hasChange.value && hasInit.value) {
    nextTick(() => {
      window.tinymce.get(tinymceId.value).setContent(val || '')
    })
  }
})

const init = () => {
  load(tinymceCDN, err => {
    if (err) {
      ElMessage.error(err.message)
      return
    }
    initTinymce()
  })
}

const emits = defineEmits(['update:modelValue'])

const initTinymce = () => {
  window.tinymce.init({
    selector: `#${tinymceId.value}`,
    language: languageTypeList.value['en'],
    height: props.height,
    body_class: 'panel-body ',
    object_resizing: false,
    toolbar: props.toolbar.length > 0 ? props.toolbar : toolbar,
    menubar: props.menubar,
    plugins: plugins,
    end_container_on_empty_block: true,
    powerpaste_word_import: 'clean',
    code_dialog_height: 450,
    code_dialog_width: 1000,
    advlist_bullet_styles: 'square',
    advlist_number_styles: 'default',
    imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
    default_link_target: '_blank',
    link_title: false,
    nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
    init_instance_callback: editor => {
      if (props.modelValue) {
        editor.setContent(props.modelValue)
      }
      hasInit.value = true
      editor.on('NodeChange Change KeyUp SetContent', () => {
        hasChange.value = true
        emits('update:modelValue', editor.getContent())
      })
    },
    setup(editor) {
      editor.on('FullscreenStateChanged', e => {
        fullscreen.value = e.state
      })
    }
  })
}

const destroyTinymce = () => {
  const tinymce = window.tinymce.get(tinymceId.value)
  if (fullscreen.value) {
    tinymce.execCommand('mceFullScreen')
  }

  if (tinymce) {
    tinymce.destroy()
  }
}
/* eslint-disable no-unused-vars */
const setContent = () => {
  window.tinymce.get(tinymceId.value).setContent(props.modelValue)
}

const getContent = () => {
  window.tinymce.get(tinymceId.value).getContent()
}

const imageSuccessCBK = arr => {
  arr.forEach(v => window.tinymce.get(tinymceId.value).insertContent(`<img class="wscnph" src="${v.url}" >`))
}

onMounted(() => {
  init()
})

onActivated(() => {
  if (window.tinymce) {
    initTinymce()
  }
})

onDeactivated(() => {
  destroyTinymce()
})

onUnmounted(() => {
  destroyTinymce()
})

</script>

<style lang="scss" scoped>
.tinymce-container {
  position: relative;
  line-height: normal;
}

.tinymce-container {
  :deep(.mce-fullscreen) {
    z-index: 10000;
  }

}

.tinymce-textarea {
  visibility: hidden;
  z-index: -1;
}

.editor-custom-btn-container {
  position: absolute;
  right: 4px;
  top: 4px;
  /*z-index: 2005;*/
}

.fullscreen .editor-custom-btn-container {
  z-index: 10000;
  position: fixed;
}

.editor-upload-btn {
  display: inline-block;
}
</style>
